<?php
if (!defined('__COLA_DIR__')) exit;

/**
 * 插件配置组件
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */
class Widget_Plugins_Config extends Widget_Abstract_Options
{
    /**
     * 插件文件路径
     *
     * @access private
     * @var string
     */
    private $_pluginFileName;

    /**
     * 插件类
     *
     * @access private
     * @var string
     */
    private $_className;

    /**
     * 获取插件信息
     *
     * @access public
     * @var array
     */
    public $info;

    /**
     * 绑定动作
     *
     * @access public
     */
    public function execute()
    {
        $this->user->pass('admin');
        $config = $this->request->filter('strip_tags')->config;
		
		$pluginInfo = $this->options->pluginInfo($config);
        if(empty($pluginInfo)){
			throw new Plugins_Exception('Missing Plugin ' . $config, 404);
		}
		
        list($this->_pluginFileName, $this->_className) = $pluginInfo;

        $this->info = Plugins::parseInfo($this->_pluginFileName);
    }

    /**
     * 获取菜单标题
     *
     * @access public
     * @return string
     */
    public function getMenuTitle()
    {
        return _t('设置插件 %s', $this->info['title']);
    }

    /**
     * 配置插件
     *
     * @access public
     * @return Form
     * @throws Widget_Exception
     */
    public function form()
    {
		

		
		
		
        /** 获取插件名称 */
        $pluginName = $this->request->filter('strip_tags')->config;

        /** 获取已启用插件 */
        $plugins = Plugins::export();

        /** 判断实例化是否成功 */
        if (!$this->info['config'] || !isset($plugins[$pluginName])) {
            throw new Widget_Exception(_t('无法配置插件'), 500);
        }

        /** 载入插件 */
        //require_once $this->_pluginFileName;
        $form = new Form($this->security->actionUrl('plugins-edit?config=' . $pluginName, false));
        call_user_func(array($this->_className, 'config'), $form);

        $options = $this->options->plugin($pluginName);

        if (!empty($options)) {
            foreach ($options as $key => $val) {
                $form->getInput($key)->value($val);
            }
        }

        $submit = new Form_Submit(NULL, NULL, _t('保存设置'));
        $form->set($submit, 'submit');
        return $form;
    }
}
